<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑角色</title>
</head>
<body>
<script type="text/html" id="form">
    <input type="hidden" name="id" value="{{ d.id }}" />
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input type="hidden" id="id" name="id" value="{{d.id}}"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名
                        <text style="color:red;">*</text>
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" value="{{d.name}}" placeholder="请输入角色名" class="layui-input"
                               lay-verify="required" autocomplete="off"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">权限设置</label>
                    <div class="layui-input-block">
                        <div id="treeview" class="laytp-tree-horizontal layui-form-mid"><i
                                class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                        </div>
                        <input type="hidden" name="menu_ids" id="node_list"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="laytp-form">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</script>
<form class="layui-form" lay-filter="layui-form"></form>
<script>
    if(localStorage.getItem("staticDomain")){
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }else{
        document.write("<link rel='stylesheet' href='/static/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }
</script>
<script>
    layui.use(['laytp'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let layer = layui.layer;
        let tree = layui.tree;
        //获取参数ID
        var id = facade.getUrlParam('id');
        if(!id){
            facade.error('参数ID不能为空');
            return false;
        }
        //获取数据，渲染到对应的节点上
        facade.ajax({
            route: "/admin.role/info",
            data: {id: id},
            successAlert: false,
            showLoading: true
        }).done(function(res){
            if(res.code === 0){
                layui.laytpl($("#form").html()).render(res.data,function(string){
                    $("form").html(string);
                    layui.laytpForm.render();
                    form.render();
                    facade.ajax({
                        route: "/admin.menu/getTree",
                        successAlert: false
                    }).done(function(res){
                        if(res.code === 0){
                            let treeData = getTreeData(res.data);
                            tree.render({
                                elem: '#treeview'
                                , data: treeData
                                , showCheckbox: true
                                , accordion: false
                                , showLine: false
                                , id: 'auth_node'
                                , oncheck: function () {
                                    var ids = facade.getTreeCheckedIds(tree.getChecked('auth_node'));
                                    $('#node_list').val(ids);
                                }
                            });
                            facade.ajax({
                                route: '/admin.role/getMenuIds',
                                data: {id: id},
                                successAlert: false,
                                async: false
                            }).done(function (res) {
                                tree.setChecked('auth_node', res.data);
                            });
                        }
                    });
                });
            }
        });

        function getTreeData(data) {
            let key;
            for (key in data) {
                data[key].title = data[key].name;
                data[key].spread = false;
                if (data[key].children != null && data[key].children.length > 0) {
                    data[key].children = getTreeData(data[key].children);
                }
            }
            return data;
        }

        form.on('submit(laytp-form)', function(data){
            var btnAnim = layui.button.load({elem:$(this)});
            facade.ajax({
                route:'/admin.role/edit',
                data:data.field
            }).done(function(res){
                if(res.code === 0){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.layui.table.reloadData("laytp-table");
                }
                btnAnim.stop();
            }).fail(function(){
                btnAnim.stop();
            });
            return false;
        });
    })
</script>
</body>
</html>